import Vue from 'vue'
//导入App组件
import App from './components/App.vue'
//导入路由器
import VueRouter from 'vue-router'
//导入路由组件
import router from './router.js'
//使用路由器
Vue.use(VueRouter)
//导入vuex
import vuex from 'vuex'
Vue.use(vuex)


const car = JSON.parse(localStorage.getItem('car')) || []

const store = new vuex.Store({
    state: {
        car: car
    },
    mutations: {
        addCar(state, goods) {
            state.car.push(goods)
            localStorage.setItem('car', JSON.stringify(state.car))
        },
        updateGoodsFromCar(state, data) {
            state.car.some(item => {
                if (item.id === data.id) {
                    item.count = data.count
                    return true
                }
            })
            localStorage.setItem('car', JSON.stringify(state.car))
        },
        removeFromCar(state, id) {
            state.car.some((item, i) => {
                if (item.id === id) {
                    state.car.splice(i, 1)
                    return true
                }
            })
            localStorage.setItem('car', JSON.stringify(state.car))
        },
        updateGoodsSelected(state, { id, selected }) {
            state.car.some(item => {
                if (item.id === id) {
                    item.selected = selected
                    return true
                }
            })
            localStorage.setItem('car', JSON.stringify(state.car))
        }
    },
    getters: {
        num(state) {
            let c = 0
            state.car.forEach(item => {
                c += item.count
            })
            return c
        },
        getGoodsSelected(state) {
            let o = {}
            state.car.forEach(item => {
                o[item.id] = item.selected
            })
            return o
        },
        getGoodsCountAndAmount(state) {
            let o = {
                count: 0,
                amount: 0
            }
            state.car.forEach(item => {
                if (item.selected) {
                    o.count += item.count
                    o.amount += item.count * item.price
                }
            })
            return o
        }
    }
})

import axios from 'axios'
import VueAxios from 'vue-axios'
axios.defaults.baseURL = 'http://localhost:9000';
Vue.use(VueAxios, axios)


//导入mui代码片段样式和字体样式
import './lib/mui/css/mui.min.css'
import './lib/mui/css/icons-extra.css'
//导入mint-ui组件
// import { Header, Swipe, SwipeItem, Button } from 'mint-ui';

// Vue.component(Header.name, Header);
// Vue.component(Swipe.name, Swipe);
// Vue.component(SwipeItem.name, SwipeItem);
// Vue.component(Button.name, Button);
import MintUI from 'mint-ui'
import 'mint-ui/lib/style.css'

Vue.use(MintUI)



new Vue({
    el: '#app',
    render: createElement => createElement(App),
    //注册路由组件
    router,
    //注册store
    store
})